package com.cy.ppj.web.admin.productannouncement;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.productannouncement.ProductAnnouncementAddAO;
import com.cy.ppj.model.ao.productannouncement.ProductAnnouncementEditAO;
import com.cy.ppj.model.domian.productannouncement.ProductAnnouncement;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.productannouncement.ProductAnnouncementAdminQuery;
import com.cy.ppj.model.vo.admin.productannouncement.ProductAnnouncementAdminVO;
import com.cy.ppj.service.productannouncement.ProductAnnouncementService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-产品与公告管理
 *
 * @author: tanyilin
 * @create: 2018年10月15日 下午5:24:13
 */
@Slf4j
@RestController
@RequestMapping("/admin/product/announcement")
@Api(tags = "后台-产品与公告管理")
public class AdminProductAnnouncementController extends BaseController{

	@Autowired
	private ProductAnnouncementService productAnnouncementService;
	
	@ApiOperation("列表")
	@GetMapping("/list")
	public PageBean<ProductAnnouncementAdminVO> list(ProductAnnouncementAdminQuery query) {
		PageBean<ProductAnnouncementAdminVO> pageBean = productAnnouncementService.selectProductAnnouncementAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("新增")
	@PostMapping("/do/add")
	public Result doAdd(@Validated ProductAnnouncementAddAO addAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		ProductAnnouncement productAnnouncement = new ProductAnnouncement();
		BeanUtils.copyProperties(addAO, productAnnouncement);
		productAnnouncementService.addProductAnnouncement(productAnnouncement);
		return ok();
	}
	
	@ApiOperation("编辑")
	@PostMapping("/do/edit")
	public Result doEdit(@Validated ProductAnnouncementEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		ProductAnnouncement productAnnouncement = new ProductAnnouncement();
		BeanUtils.copyProperties(editAO, productAnnouncement);
		productAnnouncementService.updateProductAnnouncement(productAnnouncement);
		return ok();
	}
	
	@ApiOperation("修改状态（上下架）")
	@PostMapping("/do/update/status")
	public Result updateStatus(@ApiParam(value = "ID", example = "0", required = true) @RequestParam Long id,
			@ApiParam(value = "状态", required = true) @RequestParam CommonStatus status) {
		ProductAnnouncement productAnnouncement = new ProductAnnouncement();
		productAnnouncement.setId(id);
		productAnnouncement.setStatus(status);
		productAnnouncementService.updateStatus(productAnnouncement);
		return ok();
	}
	
}
